package com.cy.linklist;

import java.util.HashMap;

/**
 * @Author: chenyun
 * @Date: 2021/09/23/17:16
 * @Description:
 */

//剑指 Offer 35. 复杂链表的复制
public class Demo35 {
    public Node copyRandomList1(Node head) {
        HashMap<Node,Node> map = new HashMap<Node,Node>();
        Node cur = head;
        //复制一份节点存map中
        while(cur != null){
            map.put(cur,new Node(cur.val));
            cur = cur.next;
        }
        cur = head;
        while(cur != null){
            //cur老
            //map.get(cur)新
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }

}
